package com.book.controller;

import com.book.entity.Account;
import com.book.service.AccountService;
import com.book.util.JsonResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * @author SuperYu
 * @date 2024/4/19
 */

@RestController
@RequestMapping("/api")
@CrossOrigin          // 设置跨域（写在方法上代表方法跨域，写在类上代表类的所有方法跨域）
public class LoginController {

    AccountService accountService;

    @Autowired
    public void setAccountService(AccountService accountService) {
        this.accountService = accountService;
    }

    @RequestMapping("/login")
    public JsonResult<Account> login(Account account) {
        JsonResult<Account> jsonResult = new JsonResult<>();

        Account loginAccount = accountService.findAccountByLogin(account.getUserName());
        if (loginAccount != null) {
            if (account.getPassword().equals(loginAccount.getPassword())) {
                jsonResult.setMessage("登录成功!");
                jsonResult.setStatus(200);
            } else {
                jsonResult.setMessage("密码有误!");
                jsonResult.setStatus(502);
            }
        } else {
            jsonResult.setMessage("用户名有误!");
            jsonResult.setStatus(501);
        }

        return jsonResult;
    }

    @RequestMapping("/showAllAccount")
    public List<Account> findAll() {
        return accountService.findAllAccounts();
    }


}
